package com.hzhr.iot.user.service;

import com.hzhr.iot.core.annotation.ApiImplicitInternalParams;
import com.hzhr.iot.core.annotation.CygQuery;
import com.hzhr.iot.core.annotation.CygQuerys;
import com.hzhr.iot.core.annotation.CygService;
import com.hzhr.iot.core.constant.AccessModeEnum;
import com.hzhr.iot.core.constant.CommonOptions;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.core.util.HzhrStringUtils;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.login.sdk.model.userCard.UserCardModel;
import com.hzhr.iot.user.Global;
import com.hzhr.iot.user.constant.User_ErrorsEnum;
import com.hzhr.iot.user.convertor.custom.DeptCustomConvertor;
import com.hzhr.iot.user.dao.DeptDao;
import com.hzhr.iot.user.dao.helper.DeptDaoHelper;
import com.hzhr.iot.user.dao.helper.UserDaoHelper;
import com.hzhr.iot.user.entity.DeptEntity;
import com.hzhr.iot.user.entity.ext.DeptEntityExt;
import com.hzhr.iot.user.model.dept_parentId.Dept_ParentIdGetModel;
import com.hzhr.iot.user.model.dept_parentId.Dept_ParentIdModel;
import com.hzhr.iot.user.model.dept_parentId.Dept_ParentIdPutModel;
import com.hzhr.iot.user.queryModel.custom.DeptCustomQueryModel;
import com.hzhr.iot.user.service.base.BaseService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@CygService(entity = DeptEntity.class, model = Dept_ParentIdModel.class)
@CygQuerys({
        @CygQuery(name = "_equal_id", query = "equal_id", type = Integer.class),
})
public class Dept_ParentIdService extends BaseService {
    @Autowired
    DeptDaoHelper deptDaoHelper;
    @Autowired
    DeptService deptService;
    @Autowired
    DeptDao deptDao;
    @Autowired
    UserDaoHelper userDaoHelper;
    @Autowired
    DeptCustomConvertor deptCustomConvertor;
    @Autowired
    Dept_ParentIdService dept_parentIdService;

    @Autowired
    Global global;

    //region javadoc注释 : putByDeptId
    /**
     * 管理员更新部门的上级编号 <br/><br/>
     * 简介&nbsp;:&nbsp;更新部门的上级部门编号 <br/>
     * 步骤&nbsp;: <br/>
     * 1.&nbsp;查询部门&nbsp;:&nbsp;(√) <br/>
     * &nbsp;根据路径参数deptId查询登录人的管辖范围下的部门(DEPT)dept_1，如果dept_1不存在，抛出异常201； <br/>
     * 2.&nbsp;查询上级部门&nbsp;:&nbsp;(√) <br/>
     * &nbsp;根据body.parentId查询登录人的管辖范围下的部门(DEPT)dept_2，如果dept_2不存在，抛出异常202； <br/>
     * 3.&nbsp;验证部门是否具备更新上级部门编号的条件&nbsp;:&nbsp;(√) <br/>
     * &nbsp;条件1：dept_1.name在dept_2的直接下级部门中不能存在，如果存在，抛出异常203； <br/>
     * &nbsp;条件2：dept_1不能存在下级部门或下级用户，如果存在，抛出异常204； <br/>
     * 4.&nbsp;更新部门信息&nbsp;:&nbsp;(√) <br/>
     * &nbsp;根据body设置dept_1的相应属性，并且同时设置以下属性： <br/>
     * &nbsp;(1)&nbsp;dept_1.parentName&nbsp;=&nbsp;dept_2.name； <br/>
     * &nbsp;(2)&nbsp;dept_1.code的设置规则： <br/>
     * &nbsp;如果dept_2不存在直接下级部门，则设置为&nbsp;dept_2.code&nbsp;+&nbsp;'01'； <br/>
     * &nbsp;如果dept_2存在直接下级部门，则查询出其直接下级部门的最大code，提取出该code的最后两位，转换为数字并在此基础上&nbsp;+1&nbsp;， <br/>
     * &nbsp;然后左补零至两位字符串str_1，最后将dept_1.code设置为&nbsp;dept_2.code&nbsp;+&nbsp;str_1； <br/>
     * &nbsp;(3)&nbsp;dept_1.resourceCode&nbsp;=&nbsp;dept_2.code&nbsp;+&nbsp;“dept_1.code(上一步生成)的最后两位”； <br/>
     * 5.&nbsp;返回信息&nbsp;:&nbsp;(√) <br/>
     * &nbsp;根据条件参数查询更新结果，组装响应并返回； <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _show : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 展示形式 one:展示一个,list:展示列表,null(空值):不展示;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _show_info : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示详情;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/><br/>
     * @param deptId 部门编号 <br/><br/>
     * @param body <br/><br/>
     * @throws ServiceException <br/>
     * @see Dept_ParentIdPutModel body <br/>
     * @see Dept_ParentIdGetModel result <br/>
     */ 
    //endregion
    //region 说明 : putByDeptId
    //管理员更新部门的上级编号
    //简介 : 更新部门的上级部门编号
    //步骤 :
    //1. 查询部门 : (√)
    // 根据路径参数deptId查询登录人的管辖范围下的部门(DEPT)dept_1，如果dept_1不存在，抛出异常201；
    //2. 查询上级部门 : (√)
    // 根据body.parentId查询登录人的管辖范围下的部门(DEPT)dept_2，如果dept_2不存在，抛出异常202；
    //3. 验证部门是否具备更新上级部门编号的条件 : (√)
    // 条件1：dept_1.name在dept_2的直接下级部门中不能存在，如果存在，抛出异常203；
    // 条件2：dept_1不能存在下级部门或下级用户，如果存在，抛出异常204；
    //4. 更新部门信息 : (√)
    // 根据body设置dept_1的相应属性，并且同时设置以下属性：
    // (1) dept_1.parentName = dept_2.name；
    // (2) dept_1.code的设置规则：
    // 如果dept_2不存在直接下级部门，则设置为 dept_2.code + '01'；
    // 如果dept_2存在直接下级部门，则查询出其直接下级部门的最大code，提取出该code的最后两位，转换为数字并在此基础上 +1 ，
    // 然后左补零至两位字符串str_1，最后将dept_1.code设置为 dept_2.code + str_1；
    // (3) dept_1.resourceCode = dept_2.code + “dept_1.code(上一步生成)的最后两位”；
    //5. 返回信息 : (√)
    // 根据条件参数查询更新结果，组装响应并返回；
    //endregion
    //region 参数 : putByDeptId
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "deptId", value = "部门编号",required = true, dataType = "int", paramType = "path"),

            @ApiImplicitParam(name = "show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),

    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),

    })

    //endregion
    //region 请求 : putByDeptId
    @RequestMapping(value = "/depts/{deptId}/dept-ids", method = RequestMethod.PUT)
    //endregion
    public Dept_ParentIdGetModel putByDeptId(Map<String, String> header, Integer deptId,
                                             Map<String, Object> query, Dept_ParentIdPutModel body) throws ServiceException {
        UserCardModel userCard = getUserCard(header);

        /* 查询部门 */
        DeptEntity dept_1 = deptDaoHelper.getByIdAndResourceCode(deptId, userCard.getUserCode());
        if (dept_1 == null) {
            throw new ServiceException(User_ErrorsEnum.DEPT_PARENTIDSERVICE_PUT_201);
        }

        /* 查询上级部门 */
        Integer parentId = body.getParentId();
        DeptEntity dept_2 = deptDaoHelper.getByIdAndResourceCode(parentId, userCard.getUserCode());
        if (dept_2 == null) {
            throw new ServiceException(User_ErrorsEnum.DEPT_PARENTIDSERVICE_PUT_202);
        }

        /* 验证部门是否具备更新上级部门编号的条件 */
        int count = deptDaoHelper.getCountByParentIdAndName(parentId, dept_1.getName());
        if (count > 0) {
            throw new ServiceException(User_ErrorsEnum.DEPT_PARENTIDSERVICE_PUT_203);
        }
        int deptId_1 = dept_1.getId();
        int count1 = deptDaoHelper.getCountByParentId(deptId_1);
        if (count1 > 0) {
            throw new ServiceException(User_ErrorsEnum.DEPT_PARENTIDSERVICE_PUT_204);
        }
        int count2 = userDaoHelper.getCountByDeptId(deptId_1);
        if (count2 > 0) {
            throw new ServiceException(User_ErrorsEnum.DEPT_PARENTIDSERVICE_PUT_204);
        }

        /* 更新部门信息 */
        dept_1.setParentId(parentId);
        dept_1.setParentName(dept_2.getName());
        int count3 = deptDaoHelper.getCountByParentId(parentId);
        if (count3 > 0) {
            DeptEntity deptEntity = deptDaoHelper.getByParentIdAndOrderByCode(parentId, "desc", 1, 1);
            String code = deptEntity.getCode();
            String subCode = code.substring(code.length() - 2);
            Integer subCodeInt = Integer.parseInt(subCode);
            subCodeInt += 1;
            String str_1 = HzhrStringUtils.fillZeroForLeft(subCodeInt.toString(), 2);
            dept_1.setCode(dept_2 + str_1);
        } else {
            dept_1.setCode(dept_2.getCompanyId() + "01");
        }
        String deptCode_1 = dept_1.getCode();
        dept_1.setResourceCode(dept_2.getResourceCode() + deptCode_1.substring(deptCode_1.length() - 2));
        dept_1.setUpdateUserId(userCard.getUserId());
        dept_1.setUpdateTime(new Date());
        int changeCount = deptDao.update(dept_1);

        /* 返回信息 */
        query.put("_equal_id", dept_1.getId());
        Dept_ParentIdGetModel result = dept_parentIdService.get(initHeader(header, AccessModeEnum.GLOBAL), query);
        result.setChangeCount(changeCount);
        return result;
    }

    //region javadoc注释 : get
    /**
     * 查询部门的上级部门编号 <br/><br/>
     * 简介&nbsp;:&nbsp;查询部门的上级部门编号 <br/>
     * 步骤&nbsp;: <br/>
     * 1.&nbsp;查询部门&nbsp;:&nbsp;(√) <br/>
     * &nbsp;根据条件参数查询部门(DEPT)； <br/>
     * 2.&nbsp;返回信息&nbsp;:&nbsp;(√) <br/>
     * &nbsp;根据条件参数和查询到的结果组装响应并返回； <br/>
     * @param header <br/><br/>
     * @param query <br/><br/>
     * @throws ServiceException <br/>
     * @see Dept_ParentIdGetModel result <br/>
     */ 
    //endregion
    //region 说明 : get
    //查询部门的上级部门编号
    //简介 : 查询部门的上级部门编号
    //步骤 :
    //1. 查询部门 : (√)
    // 根据条件参数查询部门(DEPT)；
    //2. 返回信息 : (√)
    // 根据条件参数和查询到的结果组装响应并返回；
    //endregion
    //region 参数 : get
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "show_total", value = "是否展示总数", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页数", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "count", value = "每页数量", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "equal_id", value = "筛选条件 : where dept.id = #{equal_id}",required = false, dataType = "int", paramType = "query"),

    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_show_total", value = "是否展示总数", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_page", value = "页数", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_count", value = "每页数量", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_equal_id", value = "筛选条件 : where dept.id = #{_equal_id}",required = false, dataType = "int", paramType = "query"),

    })
    //endregion
    //region 请求 : get
    @RequestMapping(value = "/depts", method = RequestMethod.GET)
    //endregion
    public Dept_ParentIdGetModel get(Map <String, String> header, Map<String, Object> query) throws ServiceException {
        /* 查询部门 */
        //生成查询模型
        DeptCustomQueryModel queryModel = deptCustomConvertor.dept_ParentIdService_query2queryModel(query);
        queryModel.setEqual_isValid(CommonOptions.YES_NO.YES);
        //查询实体
        List<DeptEntityExt> extList = null;
        String show = HzhrTypeUtil.object2string(query.get("_show"));
        if (!StringUtils.isEmpty(show)) {
            extList = deptDao.selectExtList(queryModel);
        }
        //查询数量
        Integer total = null;
        Boolean showTotal = HzhrTypeUtil.object2boolean(query.get("_show_total"));
        if (showTotal != null && showTotal) {
            total = deptDao.selectCount(queryModel);
        }

        /* 返回信息 */
        Dept_ParentIdGetModel result = deptCustomConvertor.extList2dept_ParentIdGetModel(extList, total, null, query);
        return result;
    }

}









































































































